<template>
  <d2-container>
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="data"
      :loading="loading"
      :pagination="pagination"
      :rowHandle="rowHandle"
      :add-template="template"
      :edit-template="template"
      :form-options="formOptions"
      :options="options"
      @row-add="handleRowAdd"
      @row-edit="handleRowEdit"
      @row-remove="handleRowRemove"
      @dialog-open="handleDialogOpen"
      @dialog-cancel="handleDialogCancel"
      @pagination-current-change="paginationCurrentChange">
      <el-button slot="header" style="margin: 7px" @click="addRow">新增</el-button>
    </d2-crud>
  </d2-container>
</template>

<script>
export default {
  data () {
    return {
      columns: [
        {
          title: 'ID',
          key: 'id'
        },
        {
          title: '用户ID',
          key: 'adminId'
        },
        {
          title: '角色ID',
          key: 'roleId'
        }
      ],
      data: [],
      rowHandle: {
        columnHeader: '操作',
        fixed: 'right',
        width: '200',
        edit: {
          icon: 'el-icon-edit',
          text: '编辑',
          size: 'small'
        },
        remove: {
          icon: 'el-icon-delete',
          size: 'small',
          fixed: 'right',
          confirm: true
        }
      },
      template: {
        id: {
          title: 'ID',
          value: ''
        },
        adminId: {
          title: '用户ID',
          value: '',
          component: {
            name: 'cus-select',
            props: {
              options: undefined,
              optionValue: 'id',
              optionLabel: 'username'
            }
          }
        },
        roleId: {
          title: '角色ID',
          value: '',
          component: {
            name: 'cus-select',
            props: {
              options: undefined,
              optionValue: 'id',
              optionLabel: 'nameZh'
            }
          }
        }
      },
      formOptions: {
        labelWidth: '120',
        labelPosition: 'left',
        saveLoading: false
      },
      options: {
        stripe: true
      },
      loading: false,
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },

      roleOptions: [],
      adminOptions: []
    }
  },
  mounted () {
    this.fetchData()
    this.fetchOptionData()
  },
  watch: {
    roleOptions () {
      this.template.roleId.component.props.options = this.roleOptions
    },
    adminOptions () {
      this.template.adminId.component.props.options = this.adminOptions
    }
  },
  methods: {
    paginationCurrentChange (currentPage) {
      this.pagination.currentPage = currentPage
      this.fetchData()
    },
    fetchData () {
      this.loading = true
      this.$api.modules['ums-admin-role-relation'].PAGE({
        ...this.pagination
      }).then(res => {
        this.data = res.data.list
        this.pagination.total = res.data.total
        this.loading = false
      }).catch(err => {
        console.log('err', err)
        this.loading = false
      })
      this.loading = false
    },
    fetchOptionData () {
      this.$api.modules['ums-admin'].LIST().then(res => {
        this.adminOptions = res.data
      }).catch(err => {
        console.log('err', err)
      })
      this.$api.modules['ums-role'].LIST().then(res => {
        this.roleOptions = res.data
      }).catch(err => {
        console.log('err', err)
      })
    },
    addRow () {
      this.$refs.d2Crud.showDialog({
        mode: 'add'
      })
    },
    handleRowAdd (row, done) {
      this.formOptions.saveLoading = true
      setTimeout(() => {
        this.$api.modules['ums-admin-role-relation'].ADD(row).then((response) => {
          this.open = false
          this.title = ''
          done({
            id: response.data.id
          })
          this.$message({
            message: '保存成功',
            type: 'success'
          })
        })

        this.formOptions.saveLoading = false
      }, 100)
    },
    handleRowEdit ({
      index,
      row
    }, done) {
      this.formOptions.saveLoading = true
      setTimeout(() => {
        this.$api.modules['ums-admin-role-relation'].UPDATE(row).then((response) => {
          this.open = false
          this.title = ''
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
        })
        // done可以传入一个对象来修改提交的某个字段
        done({})
        this.formOptions.saveLoading = false
      }, 100)
    },
    handleRowRemove ({
      index,
      row
    }, done) {
      setTimeout(() => {
        this.$api.modules['ums-admin-role-relation'].DELETE(row.id).then(response => {
          if (response.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            done()
          }
        })
      }, 100)
    },
    handleDialogOpen ({
      mode,
      row
    }) {

    },
    handleDialogCancel (done) {
      this.$message({
        message: '取消操作',
        type: 'warning'
      })
      done()
    }
  }
}
</script>
